package com.idlecode.panel;

import cn.hutool.core.collection.CollUtil;
import com.idlecode.bean.FieldTypeEntity;
import com.idlecode.service.FieldTypeService;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.table.JBTable;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.List;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-02-05 17:06
 **/
public class FieldTypeMappingPanel extends JPanel {
    // 创建表格模型
    private DefaultTableModel tableModel;
    private JTable table;

    public FieldTypeMappingPanel() {
        //列表显示
        JPanel bottomJPanel = new JPanel(new BorderLayout());
        tableModel = new DefaultTableModel();

        // 添加列名
        tableModel.addColumn("字段类型");
        tableModel.addColumn("属性类型");
        tableModel.addColumn("属性包名");
        List<FieldTypeEntity> fieldTypes = FieldTypeService.getFieldTypes();
        if (CollUtil.isNotEmpty(fieldTypes)) {
            for (FieldTypeEntity fieldType : fieldTypes) {
                // 添加数据行
                tableModel.addRow(new Object[]{fieldType.getColumnType(), fieldType.getAttrType(), fieldType.getPackageName()});
            }
        }
        // 创建表格
        table = new JBTable(tableModel);
        // 设置 JTable 的自动调整列宽度模式为 AUTO_RESIZE_SUBSEQUENT_COLUMNS
        // 这样可以确保在调整列宽度时，只调整之后的列宽度，而不会影响之前的列
        table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        // 获取表头
        JTableHeader header = table.getTableHeader();

        // 设置表头字段居中
        setHeaderAlignment(header, SwingConstants.CENTER);
        // 将表格放置在滚动窗格中
        JScrollPane scrollPane = new JBScrollPane(table);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        bottomJPanel.add(scrollPane);
        // 垂直排列
        setLayout(new BorderLayout());
        add(bottomJPanel, BorderLayout.CENTER);
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                int jpWidth = getWidth();
                table.setPreferredScrollableViewportSize(new Dimension(jpWidth, table.getPreferredScrollableViewportSize().height));
                // 设置列宽度比例
                setColumnProportionalWidth(table, 0, 0.3, jpWidth);
                setColumnProportionalWidth(table, 1, 0.25, jpWidth);
                setColumnProportionalWidth(table, 2, 0.45, jpWidth);
            }
        });


    }

    private static void setColumnProportionalWidth(JTable table, int columnIndex, double proportion, int jpWidth) {
        TableColumn column = table.getColumnModel().getColumn(columnIndex);
        int width = (int) (proportion * jpWidth);
        column.setPreferredWidth(width);
    }

    private static void setHeaderAlignment(JTableHeader header, int alignment) {
        for (int i = 0; i < header.getColumnModel().getColumnCount(); i++) {
            TableColumn column = header.getColumnModel().getColumn(i);
            column.setHeaderRenderer(new HeaderRenderer(alignment));
        }
    }

    static class HeaderRenderer extends DefaultTableCellRenderer {
        HeaderRenderer(int alignment) {
            setHorizontalAlignment(alignment);
        }
    }
}
